package com.lb.jwitter.action;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSo;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoGiayTo;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoGiayToService;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoService;
import com.lb.jwitter.service.TblQLDTTXAHocVienBienLaiMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienBienLaiMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiBienLai;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiBienLaiService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.tutego.jrtf.Rtf;
import com.tutego.jrtf.RtfTemplate;

public class TblQLDTTXAHocVienHocPhiBienLaiExportAction {
	private String filename;
	  private Long id;
	  
	  
	  private TblTaiKhoan taiKhoanDangNhap; 
	  public TblTaiKhoan getTaiKhoanDangNhap() { 
	      return taiKhoanDangNhap; 
	  } 
	  public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
	      this.taiKhoanDangNhap = taiKhoanDangNhap; 
	  } 
	  private TblQuyenChiTietService tblQuyenChiTietService;
	 	 @Autowired // Injected by Spring 
	      ApplicationContextProvider contextProvider; 
	 	 
	  public String execute() throws Exception {
			try{
				Map session = ActionContext.getContext().getSession();
			     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
			     if( taiKhoanDangNhap == null) return "noprivilege";
			     
			    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
			    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
			    
//			    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_BienNhanHoSo", "sua")){ 
//			        return "noprivilege"; 
//			    }        
			    
			    TblQLDTTXAHocVienHocPhiBienLaiService tblQLDTTXAHocVienHocPhiBienLaiService = (TblQLDTTXAHocVienHocPhiBienLaiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhi_BienLaiClient"); 
			    TblQLDTTXAHocVienBienLaiMonHocService tblQLDTTXAHocVienBienLaiMonHocService = (TblQLDTTXAHocVienBienLaiMonHocService) context.getBean("tbl_QLDTTXA_HocVien_BienLai_MonHocClient"); 

			    
			    if(id != null && !id.equals(new Long(0))){
			    	TblQLDTTXAHocVienHocPhiBienLai tblQLDTTXAHocVienHocPhiBienLai = tblQLDTTXAHocVienHocPhiBienLaiService.gettblQLDTTXAHocVienHocPhiBienLai(id);
				    if(tblQLDTTXAHocVienHocPhiBienLai != null){
				    	DecimalFormat myFormatter = new DecimalFormat("###,###.##");
				    	
				    	String soBienLai = tblQLDTTXAHocVienHocPhiBienLai.getSoBienLai();
				        
				    	filename ="OutFile/" + "BienLaiHocPhi" + soBienLai + ".rtf";
				    	//filename ="OutFile/" + "BienNhanHoSo" + tblQLDTTXABienNhanHoSo.getHoSoId().getMaHoSo()+ ".rtf";
				    	
						RtfTemplate rtfTemplate = Rtf.template( new FileInputStream("file/TemplateFile/bienLaiHocPhiTemplate.rtf") );
						
						rtfTemplate.inject( "soBienLai", tblQLDTTXAHocVienHocPhiBienLai.getSoBienLai() );
						rtfTemplate.inject( "hoTen", tblQLDTTXAHocVienHocPhiBienLai.getHocVienId().getHoTen().toUpperCase() + " " + tblQLDTTXAHocVienHocPhiBienLai.getHocVienId().getTen().toUpperCase());
						rtfTemplate.inject("mSSV", tblQLDTTXAHocVienHocPhiBienLai.getHocVienId().getMSHV());

						rtfTemplate.inject( "hocKy", tblQLDTTXAHocVienHocPhiBienLai.getHocKyId() == null ? "" : (tblQLDTTXAHocVienHocPhiBienLai.getHocKyId().getTen() + "/" + tblQLDTTXAHocVienHocPhiBienLai.getHocKyId().getNamHocId().getTen()) );
						rtfTemplate.inject( "soTien", myFormatter.format(tblQLDTTXAHocVienHocPhiBienLai.getSoTien()) );
								//print danh muc giay to
						List<ConditionReport> cons = new ArrayList<ConditionReport>();
						if (id != null && !id.equals(new Long(0))) 
						{ 
						       ConditionReport con = new ConditionReport(); 
						       con.setCol("bienLaiId.id"); 
						       con.setValue1(new Long(id)); 
						       con.setOperator("eq"); 
						       cons.add(con); 
						} 
						List<TblQLDTTXAHocVienBienLaiMonHoc> tblQLDTTXAHocVienBienLaiMonHocs = tblQLDTTXAHocVienBienLaiMonHocService.findtblQLDTTXAHocVienBienLaiMonHocByCondition(cons, 0, 0); 
						  
						String monHocDongHP = "";
						for(TblQLDTTXAHocVienBienLaiMonHoc tblQLDTTXAHocVienBienLaiMonHoc : tblQLDTTXAHocVienBienLaiMonHocs){
							monHocDongHP += "" + tblQLDTTXAHocVienBienLaiMonHoc.getMonHocId().getMaMonHoc() + " " + tblQLDTTXAHocVienBienLaiMonHoc.getMonHocId().getTen() + " \n";
						}
						
						rtfTemplate.inject( "monHocDongHP", monHocDongHP );
						
						rtfTemplate.inject( "ngay", tblQLDTTXAHocVienHocPhiBienLai.getNgayLap().getDay() );
						rtfTemplate.inject( "thang", tblQLDTTXAHocVienHocPhiBienLai.getNgayLap().getMonth());
						rtfTemplate.inject( "nam", tblQLDTTXAHocVienHocPhiBienLai.getNgayLap().getYear() );
						rtfTemplate.inject( "nguoiNhan", tblQLDTTXAHocVienHocPhiBienLai.getCanBoId() == null ? "" : (tblQLDTTXAHocVienHocPhiBienLai.getCanBoId().getTen().toUpperCase()) );
						
						
						
						rtfTemplate.out( new FileOutputStream("file/" +filename) );
				    }
			    }

			
			return "download"; 
			}catch(Exception e){
				 e.printStackTrace(); 
				 return Action.ERROR; 
			}
		}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
}
